<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 社区个人中心管理器
 */
class Account extends MY_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->model('bbs_account_model');
        $this->load->model('bbs_detail_model');
        $this->load->model('bbs_article_model');
        $this->load->model('bbs_user_model');
        $this->load->model('bbs_dialog_model');
        $this->load->model('bbs_notice_model');
        $this->load->model('task_model');
        if ($_POST) {
            if (!$this->userinfo) {
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "访问超时，请重新登录";
                echo JSON($mess);
                exit;
            }
        }
        if (!$this->bbs_userinfo) {//访问超时，请重新登录
            web_redirect("account/login.html" . $this->get_next_request_url());
        }
        $this->assign("uuid", encrypt_user_id($this->bbs_userinfo['user_id']));
        $this->pageSize = 10;
    }

    //社区个人中心-首页
    function index()
    {
        //我的文章
        $arts = $this->db->query("select a.id,a.title,a.summary,a.thumb,a.cat_id,a.tags_name,a.readnum,a.commentnum,a.status,a.create_time,c.name cat_name from tan_bbs_article a left join tan_bbs_article_category c  on c.id = a.cat_id "
            . " where a.authorid = " . $this->bbs_userinfo['user_id'] . " and status in(0,2) order by a.id desc ")->result_array();
        if (!$arts) {
            $arts = $this->db->query("select a.id,a.title,a.summary,a.thumb,a.cat_id,a.tags_name,a.readnum,a.commentnum,a.status,a.create_time,c.name cat_name from tan_bbs_article a left join tan_bbs_article_category c  on c.id = a.cat_id "
                . " where a.authorid = " . $this->bbs_userinfo['user_id'] . " order by a.id desc limit 5")->result_array();
        }
        foreach ($arts as $key => $val) {
            $arts[$key]['create_time'] = date("Y-m-d H:i:s", $val['create_time']);
            $arts[$key]['detail_url'] = $val['status'] == 1 ? web_url("p/" . encrypt_article_id($val['id'])) : web_url("/p/upub/" . encrypt_article_id($val['id']));
            $arts[$key]['cat_url'] = web_url("p/cat-" . $val['cat_id']);
            $arts[$key]['edit_url'] = web_url("post/edit/" . encrypt_article_id($val['id']));
            $tags = array();
            if ($val['tags_name']) {
                $tags_arr = explode(",", $val['tags_name']);
                foreach ($tags_arr as $v) {
                    $v = explode("_", $v);
                    $tags[$v[1]] = array("tid" => $v[1], "tname" => $v[0], "turl" => web_url("p/tag-" . $v[1]));
                }
            }
            $arts[$key]['tags'] = $tags;
        }

        //我的问答
        $wendas = $this->db->select("id,title,answernum,tags_name,create_time,answer_id")->from("bbs_wenda")->where(array("questioner_id" => $this->bbs_userinfo['user_id'], "answer_id" => 0))->order_by("id desc")->get()->result_array();
        foreach ($wendas as $key => $val) {
            //最新回答
            $new_answer = $this->db->select("id,content")->from("bbs_wenda_answer")->where(array("wenda_id" => $val['id'], "is_show" => 1))->order_by("id desc")->get()->row_array();
            $wendas[$key]['create_time'] = $this->set_time_two($val['create_time']);
            $wendas[$key]['detail_url'] = web_url("wenda/" . encrypt_wenda_id($val['id']));
            $wendas[$key]['answer_content'] = $new_answer ? mb_strcut(strip_tags(trim($new_answer['content'])), 0, 100) : "";
            $wendas[$key]['answer_url'] = $new_answer ? web_url("wenda/" . encrypt_wenda_id($val['id']) . "/answer-" . encrypt_answer_id($new_answer['id'])) : "";
            $tags = array();
            if ($val['tags_name']) {
                $tags_arr = explode(",", $val['tags_name']);
                foreach ($tags_arr as $v) {
                    $v = explode("_", $v);
                    $tags[$v[1]] = array("tid" => $v[1], "tname" => $v[0], "turl" => web_url("wenda/tag-" . $v[1]));
                }
            }
            $wendas[$key]['tags'] = $tags;
        }
        //我的评论
        $comments = $this->db->select("c.*,a.title,a.commentnum,u1.nickname authorid_nickname,u1.head_img authorid_head_img,u2.nickname reply_nickname")
            ->from("bbs_article_comment c")
            ->join("users_info u1", "u1.user_id = c.authorid", "left")
            ->join("users_info u2", "u2.user_id = c.repay_user_id", "left")
            ->join("bbs_article a", "a.id = c.article_id", "left")
            ->where(array("c.uid" => $this->bbs_userinfo['user_id'], "c.is_show" => 1))->order_by("id desc")->limit(5)->get()->result_array();

        foreach ($comments as $key => $val) {
            $comments[$key]['create_time'] = date("Y-m-d H:i:s", $val['create_time']);
            $comments[$key]['comment_url'] = web_url("p/" . encrypt_article_id($val['article_id']) . "/comments/" . encrypt_comment_id($val['id']));
            $comments[$key]['reply_url'] = $val['repay_user_id'] ? web_url("u/" . encrypt_user_id($val['repay_user_id'])) : "";
            $comments[$key]['author_url'] = web_url("u/" . encrypt_user_id($val['authorid']));
            $comments[$key]['comment_id'] = encrypt_comment_id($val['id']);
        }

        //未读消息
        $where_in = "'event_reg','event_focus_author'";
        $where_in .= ",'event_add_article_care','event_collect_article','event_reply_article','event_like_article','event_comment_article','event_reward_article'";
        $where_in .= ",'event_comment_wenda','event_comment_wenda_care','event_reply_wenda','event_like_answer','event_care_answer','event_best_answer','event_add_wenda_care'";
        $notices = $this->db->query("select * from tan_bbs_notice where recieve_id in (" . $this->bbs_userinfo['user_id'] . ",0) and event_type in($where_in) and is_read not like '%" . $this->bbs_userinfo['user_id'] . "%' order by id desc limit 5")->result_array();
        $notices = $this->set_myself_notice($notices);
        $this->left_info();
        $this->assign("arts", $arts);
        $this->assign("wendas", $wendas);
        $this->assign("comments", $comments);
        $this->assign("notices", $notices);
        $this->display('account/index.html');
    }

    //社区个人中心-左侧公共信息
    private function left_info()
    {
        $base_info = $this->bbs_userinfo;
        $base_info['signature_tip'] = $base_info['signature'] ? $base_info['signature'] : "";
        $base_info['signature'] = $base_info['signature'] ? $base_info['signature'] : "我好懒，什么也没写～";
        $bbs_count = $this->bbs_user_model->get_bbs_count($this->bbs_userinfo['user_id']);
        $base_info['following_count'] = $bbs_count['following_count'];
        $base_info['fans_count'] = $bbs_count['fans_count'];
        $base_info['articles_count'] = $bbs_count['articles_count'];
        $base_info['reply_count'] = $bbs_count['reply_count'];
        $base_info['visitors_count'] = $bbs_count['visitors_count'];
        $today_visitors_count = $this->db->select("SUM(num) num")->from("bbs_visit_list")->where(array("space_uid" => $this->bbs_userinfo['user_id'], "day" => date("Ymd")))->order_by("create_time")->get()->row_array();
        $base_info['today_visitors_count'] = $today_visitors_count['num'] ? $today_visitors_count['num'] : 0;

        $this->assign("baseinfo", $base_info);
    }

    //社区签到
    function sign()
    {

        //加载日历类
        $this->load->library('MCalendar');
        $MCalendar = new MCalendar();
        $calendar = $MCalendar->display();
        $this_year = date("Y");
        $this_month = date("m");
        //获取上个月签到的天数
        $where = array();
        $where['year_time'] = $this_month == "01" ? $this_year - 1 : $this_year;
        $where['month_time'] = $this_month == "01" ? "12" : $this_month - 1;
        $where['user_id'] = $this->bbs_userinfo['user_id'];
        $pre_signs = $this->users_model->get_user_sign($where);
        $pre_sign_days = "";
        if ($pre_signs) {
            foreach ($pre_signs as $key => $val) {
                $pre_sign_days .= $val['day_time'] . ",";
            }
        }
        //获取当前月签到的天数
        $where = array();
        $where['year_time'] = $this_year;
        $where['month_time'] = $this_month;
        $where['user_id'] = $this->bbs_userinfo['user_id'];
        $current_signs = $this->users_model->get_user_sign($where);
        $current_sign_days = "";
        if ($current_signs) {
            foreach ($current_signs as $key => $val) {
                $current_sign_days .= $val['day_time'] . ",";
            }
        }
        //获取今日签到数据
        $sign_today = $this->users_model->get_user_sign(array("user_id" => $this->bbs_userinfo['user_id'], "year_time" => date("Y"), "month_time" => date("m"), "day_time" => date("d")));
        $sign_today_data = $sign_today ? $sign_today[0] : "";
        $this->assign("sign_today_data", $sign_today_data);
        $this->assign("current_month", $this_year . "年" . $this_month . "月");
        $this->assign("pre_sign_days", $pre_sign_days ? substr($pre_sign_days, 0, -1) : "");
        $this->assign("current_sign_days", $current_sign_days ? substr($current_sign_days, 0, -1) : "");
        $this->assign("mark_flag", $this->bbs_userinfo['mark_time'] ? (date("Y-m-d", $this->bbs_userinfo['mark_time']) == date("Y-m-d") ? 0 : 1) : 1);
        $this->assign("calendar", $calendar);
        $this->display('account/sign.html');
    }

    //社区个人中心-我的文章
    function arts()
    {
        $count = $this->db->select("SUM(commentnum) commentnum,SUM(readnum) readnum")->from("bbs_article")->where(array("authorid" => $this->bbs_userinfo['user_id'], "status" => 1))->order_by("create_time")->get()->row_array();
        $this->left_info();
        $this->assign("count", $count);
        $this->display('account/arts.html');
    }

    //我的收藏
    function collects($pageIndex = 1)
    {
        $this->left_info();
        $this->display('account/collects.html');
    }

    //我的打赏收入
    function post_awardin($pageIndex = 1)
    {
        $this->article_statistics();
        $totalCount = $this->bbs_account_model->get_article_user_award_count(array('authorid' => $this->bbs_userinfo['user_id']));
        $page = new Paging($totalCount, $this->pageSize);
        $list = $this->bbs_account_model->get_article_user_award_list(array('tan_bbs_reward.authorid' => $this->bbs_userinfo['user_id']), ($pageIndex - 1) * $this->pageSize, $this->pageSize);
        foreach ($list as $key => $val) {
            $cat = $this->bbs_article_model->get_article_category(array("id" => $val['cat_id']));
            $list[$key]["cat_url"] = web_url($cat['group'] . "/c/" . $cat['code']);
            $list[$key]["cat_name"] = $cat['name'];
            $list[$key]['create_time'] = date("m月d日 H:s", $val['create_time']);
            $list[$key]["space_url"] = web_url("space/" . $val['user_id']);
            $list[$key]["detail_url"] = web_url("detail/" . $val['article_id']);
        }
        $this->rightinfo();
        $this->assign("list", $list);
        $this->assign('showPage', $page->showpage());
        $this->display('account/index.html');
    }

    //我的打赏支出
    function post_awardout()
    {
        $this->article_statistics();
        $totalCount = $this->bbs_account_model->get_article_user_award_count(array('user_id' => $this->bbs_userinfo['user_id']));
        $page = new Paging($totalCount, $this->pageSize);
        $list = $this->bbs_account_model->get_article_user_award_list(array('tan_bbs_reward.user_id' => $this->bbs_userinfo['user_id']), ($pageIndex - 1) * $this->pageSize, $this->pageSize);
        foreach ($list as $key => $val) {
            $cat = $this->bbs_article_model->get_article_category(array("id" => $val['cat_id']));
            $list[$key]["cat_url"] = web_url($cat['group'] . "/c/" . $cat['code']);
            $list[$key]["cat_name"] = $cat['name'];
            $list[$key]['create_time'] = date("m月d日 H:s", $val['create_time']);
            $list[$key]["space_url"] = web_url("space/" . $val['authorid']);
            $list[$key]["detail_url"] = web_url("detail/" . $val['article_id']);
        }
        $this->rightinfo();
        $this->assign("list", $list);
        $this->assign('showPage', $page->showpage());
        $this->display('account/index.html');
    }

    //溜珠明细
    function liuzhu()
    {
        $pageIndex = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $totalCount = $this->bbs_account_model->get_zhu_count(array('user_id' => $this->bbs_userinfo['user_id']));
        $page = new Paging($totalCount, $this->pageSize);
        $list = $this->bbs_account_model->get_zhu_list(array('user_id' => $this->bbs_userinfo['user_id']), ($pageIndex - 1) * $this->pageSize, $this->pageSize);
        foreach ($list as $key => $val) {
            $list[$key]['create_time'] = date("Y-m-d H:i:s", $val['create_time']);
        }
        $this->assign("list", $list);
        $this->assign('showPage', $page->showpage());
        $this->display('account/liuzhu.html');
    }

    //我的关注
    function follows()
    {
        $this->display('account/friend.html');
    }

    //我的粉丝
    function fans()
    {
        $this->display('account/friend.html');
    }

    //我的私信
    function letter()
    {
        $this->display('account/letter.html');
    }

    //系统消息
    function msg_sys()
    {

        $pageIndex = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $totalCount = $this->db->query("select * from tan_bbs_notice where recieve_id in (" . $this->bbs_userinfo['user_id'] . ",0) and type = 'system' order by id desc")->num_rows();
        $page = new Paging($totalCount, $this->pageSize);
        $list = $this->db->query("select * from tan_bbs_notice where recieve_id in (" . $this->bbs_userinfo['user_id'] . ",0) and type = 'system' order by id desc limit " . (($pageIndex - 1) * $this->pageSize) . "," . $this->pageSize)->result_array();
        $list = $this->set_myself_notice($list);
        foreach ($list as $key => $val) {
            if (!$val['is_read_for']) {
                $val['is_read'] = $val['is_read'] . "," . $this->bbs_userinfo['user_id'];
                $this->db->query("update tan_bbs_notice set is_read = '" . $val['is_read'] . "' where id = " . $val['id']);
            }
        }
        $this->assign("list", $list);
        $this->assign('showPage', $page->showpage());
        $this->display('account/message_system.html');
    }

    //文章消息
    function msg_art()
    {
        $pageIndex = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $where_in = "'event_add_article_care','event_collect_article','event_reply_article','event_like_article','event_comment_article','event_reward_article'";
        $totalCount = $this->db->query("select * from tan_bbs_notice where recieve_id = " . $this->bbs_userinfo['user_id'] . " and event_type in($where_in) and type = 'article' order by id desc")->num_rows();
        $page = new Paging($totalCount, $this->pageSize);
        $list = $this->db->query("select * from tan_bbs_notice where recieve_id = " . $this->bbs_userinfo['user_id'] . " and event_type in($where_in) and type = 'article' order by id desc limit " . (($pageIndex - 1) * $this->pageSize) . "," . $this->pageSize)->result_array();
        $list = $this->set_myself_notice($list);
        foreach ($list as $key => $val) {
            if (!$val['is_read_for']) {
                $val['is_read'] = $val['is_read'] . "," . $this->bbs_userinfo['user_id'];
                $this->db->query("update tan_bbs_notice set is_read = '" . $val['is_read'] . "' where id = " . $val['id']);
            }
        }
        $this->assign("list", $list);
        $this->assign('showPage', $page->showpage());
        $this->display('account/message_art.html');
    }

    //问答消息
    function msg_wenda()
    {
        $pageIndex = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $where_in = "'event_comment_wenda','event_comment_wenda_care','event_reply_wenda','event_like_answer','event_care_answer','event_best_answer','event_add_wenda_care'";
        $totalCount = $this->db->query("select * from tan_bbs_notice where recieve_id in (" . $this->bbs_userinfo['user_id'] . ",0) and event_type in($where_in) and type = 'wenda' order by id desc")->num_rows();
        $page = new Paging($totalCount, $this->pageSize);
        $list = $this->db->query("select * from tan_bbs_notice where recieve_id in (" . $this->bbs_userinfo['user_id'] . ",0) and event_type in($where_in) and type = 'wenda' order by id desc limit " . (($pageIndex - 1) * $this->pageSize) . "," . $this->pageSize)->result_array();
        $list = $this->set_myself_notice($list);
        foreach ($list as $key => $val) {
            if (!$val['is_read_for']) {
                $val['is_read'] = $val['is_read'] . "," . $this->bbs_userinfo['user_id'];
                $this->db->query("update tan_bbs_notice set is_read = '" . $val['is_read'] . "' where id = " . $val['id']);
            }
        }
        $this->assign("list", $list);
        $this->assign('showPage', $page->showpage());
        $this->display('account/message_wenda.html');
    }


    //我的夺宝记录
    function duobao_index()
    {
        $this->rightinfo();
        $this->display("account/duobao.html");
    }

    //我的夺宝记录
    function duobao_finish()
    {
        $this->rightinfo();
        $this->display("account/duobao.html");
    }

    //我的夺宝记录
    function duobao_record()
    {
        $this->rightinfo();
        $this->display("account/duobao.html");
    }

    //我的提问
    function question()
    {
        $this->wenda_top();
        $this->assign("action", "question");
        $this->assign("act", "question");
        $this->display("account/wenda.html");
    }

    //我的回答
    function answer()
    {
        $this->wenda_top();
        $this->assign("action", "question");
        $this->assign("act", "answer");
        $this->display("account/wenda.html");
    }

    //我的关注问题
    function follow_question()
    {
        $this->wenda_top();
        $this->assign("action", "question");
        $this->assign("act", "follow_question");
        $this->display("account/wenda.html");
    }

    private function wenda_top()
    {
        $bbs_count = $this->db->select("question_count,answer_count,answer_best_count,follow_question_count")->from("bbs_count")->where(array("user_id" => $this->bbs_userinfo['user_id']))->order_by("id desc")->get()->row_array();
        $this->assign("count", $bbs_count);
    }

    //我的曝光
    function exposure()
    {
        $this->exposure_top();
        $this->assign("action", "exposure");
        $this->assign("act", "exposure");
        $this->display("account/exposure.html");
    }

    //我的曝光回复
    function exposure_re()
    {
        $this->exposure_top();
        $this->assign("action", "exposure");
        $this->assign("act", "exposure_re");
        $this->display("account/exposure.html");
    }

    //我的关注曝光
    function follow_exposure()
    {
        $this->exposure_top();
        $this->assign("action", "exposure");
        $this->assign("act", "follow_exposure");
        $this->display("account/exposure.html");
    }
    //我的无忧回复
    function information_re()
    {
        $this->exposure_top();
        $this->assign("action", "exposure");
        $this->assign("act", "exposure_re");
        $this->display("account/exposure.html");
    }

    private function exposure_top()
    {
        $count['exposure_num'] = $this->db->query("select id from tan_bbs_exposure where questioner_id=".$this->userinfo['user_id'])->num_rows();
        $count['exposure_re_num'] = $this->db->query("select id from tan_bbs_exposure_answer where user_id=".$this->userinfo['user_id'])->num_rows();
        $count['follow_exposure_num'] = $this->db->query("select id from tan_bbs_exposure_care where user_id=".$this->userinfo['user_id'])->num_rows();

        $this->assign("count", $count);
    }


    //社区个人中心-我的评论
    function comment()
    {
        $this->left_info();
        $this->display("account/comment.html");
    }

    function awardin()
    {
        $pageIndex = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $totalCount = $this->db->select("*")->from("bbs_reward")->where(array('authorid' => $this->bbs_userinfo['user_id'], 'target_type' => 'article'))->order_by("id desc")->get()->num_rows();
        $page = new Paging($totalCount, $this->pageSize);
        $list = $this->db->query("select r.*,u.head_img,u.nickname,a.title from tan_bbs_reward r left join tan_users_info u on u.user_id = r.user_id left join tan_bbs_article a on a.id = r.target_id where r.authorid = " . $this->bbs_userinfo['user_id'] . " order by r.id desc limit " . (($pageIndex - 1) * $this->pageSize) . "," . $this->pageSize)->result_array();
        foreach ($list as $key => $val) {
            $list[$key]['detail_url'] = web_url("p/" . encrypt_article_id($val['target_id']));
            $list[$key]['space_url'] = web_url("u/" . encrypt_user_id($val['user_id']));
            $list[$key]['create_time'] = date("Y-m-d H:i:s", $val['create_time']);
        }
        $this->assign("list", $list);
        $this->assign('showPage', $page->showpage());
        $this->display("account/awardin.html");
    }

    function awardout()
    {
        $pageIndex = isset($_REQUEST['page']) ? trim($_REQUEST['page']) : 1;
        $totalCount = $this->db->select("*")->from("bbs_reward")->where(array('user_id' => $this->bbs_userinfo['user_id'], 'target_type' => 'article'))->order_by("id desc")->get()->num_rows();
        $page = new Paging($totalCount, $this->pageSize);
        $list = $this->db->query("select r.*,u.head_img,u.nickname,a.title from tan_bbs_reward r left join tan_users_info u on u.user_id = r.authorid left join tan_bbs_article a on a.id = r.target_id where r.user_id = " . $this->bbs_userinfo['user_id'] . " order by r.id desc limit " . (($pageIndex - 1) * $this->pageSize) . "," . $this->pageSize)->result_array();
        foreach ($list as $key => $val) {
            $list[$key]['detail_url'] = web_url("p/" . encrypt_article_id($val['target_id']));
            $list[$key]['space_url'] = web_url("u/" . encrypt_user_id($val['authorid']));
            $list[$key]['create_time'] = date("Y-m-d H:i:s", $val['create_time']);
        }
        $this->assign("list", $list);
        $this->assign('showPage', $page->showpage());
        $this->display("account/awardout.html");
    }

    //我发布的任务
    function taskrel(){

        $this->task_top();
        $this->assign("action", "taskrel");
        $this->assign("act", "taskrel");
        $this->display("account/task.html");
    }
    //我发布的任务领取记录
    function taskreclog(){
        $this->task_top();

        $taskid = isset($_GET['tid']) ? trim($_GET['tid']) : 0;
        $taskinfo = $this->task_model->task_detail(decrypt_wenda_id($taskid));
        $taskinfo['add_time'] = date('Y-m-d H:i:s',$taskinfo['add_time']);
        $this->assign("taskinfo", $taskinfo);
        $this->assign("task_id", $taskid);
        $this->assign("action", "taskreclog");
        $this->assign("act", "taskrel");
        $this->display("account/task.html");
    }
    //我发布的任务领取记录all
    function taskrecalllog(){
        $this->task_top();

        $this->assign("action", "taskrecalllog");
        $this->assign("act", "taskrel");
        $this->display("account/task.html");
    }
    //我发布的任务领取记录
    function taskreclogdetail(){
        $this->task_top();

        $sid = isset($_GET['sid']) ? trim($_GET['sid']) : 0;
        $subtask_detail = $this->db->query("select ts.*,u.username from tan_subtasks ts left join tan_users u on ts.uid = u.user_id where ts.sid={$sid}")->row_array();
        if(!empty($subtask_detail)){

            $subtask_detail['receive_time'] = empty($subtask_detail['receive_time'])?"未提交":date('Y-m-d H:i:s',$subtask_detail['receive_time']);
            $subtask_detail['check_time']   = empty($subtask_detail['check_time'])?"未验证":date('Y-m-d H:i:s',$subtask_detail['check_time']);
            $subtask_detail['add_time']     = date('Y-m-d H:i:s',$subtask_detail['add_time']);
            if ($subtask_detail['status']==0){
                $subtask_detail['status_text']   = "";
            }else if ($subtask_detail['status']==1){
                $subtask_detail['status_text']   = "验证通过";
            }else if ($subtask_detail['status']==2){
                $subtask_detail['status_text']   = "未通过";
            }else if ($subtask_detail['status']==3){
                $subtask_detail['status_text']   = "超时失败";
            }else if ($subtask_detail['status']==4){
                $subtask_detail['status_text']   = "已取消";
            }
            //处理validate:反序列化，
            $validate = unserialize($subtask_detail['validate']);
            $subtask_detail['name'] = isset($validate['name'])?$validate['name']:"";
            $subtask_detail['identity'] = isset($validate['identity'])?$validate['identity']:"";
            $subtask_detail['phone'] = isset($validate['phone'])?$validate['phone']:"";
            $subtask_detail['img_list'] = isset($validate['img_list'])?$validate['img_list']:"";
            unset($subtask_detail['validate']);
        }
        $this->assign("detail", $subtask_detail);
        $this->assign("sid", $sid);
        $this->assign("action", "taskreclogdetail");
        $this->assign("act", "taskrel");
        $this->display("account/task.html");
    }

    //我领取的任务
    function taskrec(){
        $this->task_top();

        $this->assign("action", "taskrec");
        $this->assign("act", "taskrec");
        $this->display("account/task.html");
    }
    //我领取的任务申诉
    function taskappeal(){
        $this->task_top();
        $subid = isset($_GET['sid']) ? trim($_GET['sid']) : 0;
        $subid = decrypt_wenda_id($subid);
        $appeal_info = $this->db->query("select c.*,u.username as initiator from tan_complaint c left join tan_users u on u.user_id=c.uid where c.sid=$subid")->row_array();
        $subtask_info =  $this->db->query("select * from tan_subtasks where sid=$subid")->row_array();
        $web_config = $this->task_model->website_config();
        if ($appeal_info){

            $task_info =  $this->db->query("select t.*,u.username as publisher from tan_task t left join tan_users u on u.user_id=t.uid where t.tid=".$subtask_info['tid'])->row_array();
            $comm_info =  $this->db->query("select cc.*,u.username from tan_complaint_comm cc left join tan_users u on u.user_id=cc.uid where cc.cid=".$appeal_info['cid']." order by cc.id desc")->result_array();

            $task_info['detail_url'] = 'http://'.$_SERVER['HTTP_HOST']."/task/".$task_info['tid'].".html";

            $subtask_info['add_time'] = $subtask_info["add_time"]?date("Y-m-d H:i:s",$subtask_info["add_time"]):'';
            $subtask_info['receive_time'] = $subtask_info["receive_time"]?date("Y-m-d H:i:s",$subtask_info["receive_time"]):'';
            $subtask_info['check_time'] = $subtask_info["check_time"]?date("Y-m-d H:i:s",$subtask_info["check_time"]):'';

            switch ($appeal_info['status']){
                case 0:
                    $appeal_info['status_val'] = "<font color='#00f'>申诉中</font>";
                    break;
                case 1:
                    $appeal_info['status_val'] = "<font color='#f0f'>发布方拒绝申诉</font>";
                    //申请客服介入时间倒计时，当前时间减去拒绝时间（从申诉表获取）
                    $task_receiver_period = $web_config['task_receiver_period'];//单位：小时
                    $time_dif = $task_receiver_period*3600 - (time() - $appeal_info['check_time']);
                    $left_time = $time_dif < 0 ? 0 : $time_dif;
                    $appeal_info['task_receiver_period'] = $left_time;
                    break;
                case 2:
                    $appeal_info['status_val'] = "<font color='#4dab0e'>申诉成功</font>";
                    break;
                case 4:
                    $appeal_info['status_val'] = "<font color='#00f'>取消申诉</font>";
                    break;
                case 5:
                    $appeal_info['status_val'] = "<font color='#f00'>申诉失败</font>";
                    break;
            }

            foreach ($comm_info as $item=>$value){
                if ($value['username']==''){
                    $comm_info[$item]['username'] = "<font color='#4dab0e'>客服</font>";
                }
                if ($value['uid']==$appeal_info['uid']){
                    $comm_info[$item]['username'] = "<font color='#f00'>".$value['username']."</font>";
                }
                if ($value['uid']==$task_info['uid']){
                    $comm_info[$item]['username'] = "<font color='#00f'>".$value['username']."</font>";
                }
                $comm_info[$item]['add_time'] = $value["add_time"]?date("Y-m-d H:i:s",$value["add_time"]):'';
            }

            $this->assign('appeal_info',$appeal_info);
            $this->assign('subtask_info',$subtask_info);
            $this->assign('task_info',$task_info);
            $this->assign('comm_info',$comm_info);
            $this->assign('cid',encrypt_wenda_id($appeal_info['cid']));
        }

        $this->assign("tid", $subtask_info['tid']);
        $this->assign("sid", $_GET['sid']);
        $this->assign("action", "taskappeal");
        $this->assign("act", "taskrec");
        $this->display("account/task.html");
    }
    //我发布的任务申诉回复
    function taskreappeal(){
        $this->task_top();
        $subid = isset($_GET['sid']) ? trim($_GET['sid']) : 0;
        $subid = decrypt_wenda_id($subid);
        $appeal_info = $this->db->query("select c.*,u.username as initiator from tan_complaint c left join tan_users u on u.user_id=c.uid where c.sid=$subid")->row_array();
        $subtask_info =  $this->db->query("select * from tan_subtasks where sid=$subid")->row_array();

        if ($appeal_info){

            $task_info =  $this->db->query("select t.*,u.username as publisher from tan_task t left join tan_users u on u.user_id=t.uid where t.tid=".$subtask_info['tid'])->row_array();
            $comm_info =  $this->db->query("select cc.*,u.username from tan_complaint_comm cc left join tan_users u on u.user_id=cc.uid where cc.cid=".$appeal_info['cid']." order by cc.id desc")->result_array();

            $task_info['detail_url'] = 'http://'.$_SERVER['HTTP_HOST']."/task/".$task_info['tid'].".html";

            $subtask_info['add_time'] = $subtask_info["add_time"]?date("Y-m-d H:i:s",$subtask_info["add_time"]):'';
            $subtask_info['receive_time'] = $subtask_info["receive_time"]?date("Y-m-d H:i:s",$subtask_info["receive_time"]):'';
            $subtask_info['check_time'] = $subtask_info["check_time"]?date("Y-m-d H:i:s",$subtask_info["check_time"]):'';

            switch ($appeal_info['status']){
                case 0:
                    $appeal_info['status_val'] = "<font color='#00f'>申诉中</font>";
                    break;
                case 1:
                    $appeal_info['status_val'] = "<font color='#f0f'>发布方拒绝申诉</font>";
                    break;
                case 2:
                    $appeal_info['status_val'] = "<font color='#4dab0e'>申诉成功</font>";
                    break;
                case 4:
                    $appeal_info['status_val'] = "<font color='#00f'>取消申诉</font>";
                    break;
                case 5:
                    $appeal_info['status_val'] = "<font color='#f00'>申诉失败</font>";
                    break;
            }

            foreach ($comm_info as $item=>$value){
                if ($value['username']==''){
                    $comm_info[$item]['username'] = "<font color='#4dab0e'>客服</font>";
                }
                if ($value['uid']==$appeal_info['uid']){
                    $comm_info[$item]['username'] = "<font color='#f00'>".$value['username']."</font>";
                }
                if ($value['uid']==$task_info['uid']){
                    $comm_info[$item]['username'] = "<font color='#00f'>".$value['username']."</font>";
                }
                $comm_info[$item]['add_time'] = $value["add_time"]?date("Y-m-d H:i:s",$value["add_time"]):'';
            }

            $this->assign('appeal_info',$appeal_info);
            $this->assign('subtask_info',$subtask_info);
            $this->assign('task_info',$task_info);
            $this->assign('comm_info',$comm_info);
            $this->assign('cid',encrypt_wenda_id($appeal_info['cid']));
        }

        $this->assign("tid", encrypt_wenda_id($subtask_info['tid']));
        $this->assign("sid", $_GET['sid']);
        $this->assign("action", "taskreappeal");
        $this->assign("act", "taskrel");
        $this->display("account/task.html");
    }


    private function task_top()
    {
        $taskrec = $this->db->query("select sum(t.commission) AS taskrecM_count,COUNT(*) as taskrecend_count from tan_subtasks st left join tan_task t on t.tid=st.tid where st.status=1 and st.is_end=1 and st.uid=".$this->userinfo['user_id'])->row_array();
        $task_count['taskrecM_count'] = $taskrec['taskrecM_count'];
        $task_count['taskrecend_count'] = $taskrec['taskrecend_count'];
        $taskrel = $this->db->query("select COUNT(*) as taskrel_count from tan_task t where t.uid=".$this->userinfo['user_id'])->row_array();
        $task_count['taskrel_count'] = $taskrel['taskrel_count'];
        $taskrel = $this->db->query("select COUNT(*) as taskrec_count from tan_subtasks st where st.uid=".$this->userinfo['user_id'])->row_array();
        $task_count['taskrec_count'] = $taskrel['taskrec_count'];

        $this->assign("count", $task_count);
    }
}